<template>
  <el-card>
    <!-- 标题 + 新增 -->
    <template #header>
      <div class="flex-between">
        <span>非药品收费项目管理</span>
        <el-button type="primary" @click="openDialog()">新增项目</el-button>
      </div>
    </template>

    <!-- 查询区 -->
    <el-form :inline="true" class="mb-3">
      <el-form-item label="关键字">
        <el-input
            v-model="query.keyword"
            placeholder="收费编码 / 名称"
            clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="loadData()">查询</el-button>
        <el-button @click="reset()">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 列表（已删除 ID 列） -->
    <el-table :data="tableData" stripe border style="width:100%">
      <el-table-column prop="expcode"  label="收费编码" width="120" />
      <el-table-column prop="expname"  label="收费名称" />
      <el-table-column label="操作" width="180">
        <template #default="{ row }">
          <el-button size="small" type="primary" @click="openDialog(row)">编辑</el-button>
          <el-button size="small" type="danger" @click="remove(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="mt-3 text-right">
      <el-pagination
          :current-page="query.page"
          :page-size="query.pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          :page-sizes="[5,10,20,50]"
          @current-change="p => { query.page = p; loadData() }"
          @size-change="s => { query.pageSize = s; loadData() }"
      />
    </div>
  </el-card>

  <!-- 新增 / 编辑对话框 -->
  <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      width="500px"
      @close="formRef?.clearValidate()"
  >
    <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
      <el-form-item prop="expcode" label="收费编码">
        <el-input v-model="form.expcode" />
      </el-form-item>
      <el-form-item prop="expname" label="收费名称">
        <el-input v-model="form.expname" />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="save()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios'
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus'

/* 统一前缀 */
const entity = 'service'

/* 查询参数 */
const query = reactive({ keyword: '', page: 1, pageSize: 10 })
const tableData = ref<any[]>([])
const total = ref(0)

/* 对话框相关 */
const dialogVisible = ref(false)
const dialogTitle = ref('新增项目')
const form = reactive<any>({ id: undefined, expcode: '', expname: '' })
const formRef = ref<FormInstance>()

/* 校验规则 */
const rules = {
  expcode: [{ required: true, message: '请输入收费编码', trigger: 'blur' }],
  expname: [{ required: true, message: '请输入收费名称', trigger: 'blur' }],
}

/* ==================== CRUD ==================== */
async function loadData() {
  const { data } = await axios.get(`/api/master/${entity}`, { params: query })
  tableData.value = data.records
  total.value = data.total
}

function reset() {
  Object.assign(query, { keyword: '', page: 1 })
  loadData()
}

function openDialog(row?: any) {
  if (row) {
    Object.assign(form, row)
    dialogTitle.value = '编辑项目'
  } else {
    Object.assign(form, { id: undefined, expcode: '', expname: '' })
    dialogTitle.value = '新增项目'
  }
  dialogVisible.value = true
}

async function save() {
  await formRef.value?.validate()
  if (form.id) {
    await axios.put(`/api/master/${entity}/${form.id}`, form)
    ElMessage.success('更新成功')
  } else {
    await axios.post(`/api/master/${entity}`, form)
    ElMessage.success('新增成功')
  }
  dialogVisible.value = false
  loadData()
}

async function remove(row: any) {
  try {
    await ElMessageBox.confirm('确认删除该项目吗？', '提示', { type: 'warning' })
    await axios.delete(`/api/master/${entity}/${row.id}`)
    if (tableData.value.length === 1 && query.page > 1) {
      query.page--
    }
    ElMessage.success('删除成功')
    loadData()
  } catch {
    /* 取消或失败不提示 */
  }
}

onMounted(loadData)
</script>

<style scoped>
.flex-between { display: flex; justify-content: space-between; align-items: center; }
.mb-3         { margin-bottom: 12px; }
.mt-3         { margin-top: 12px; }
.text-right   { text-align: right; }
</style>
